#!/bin/bash

#暂不启用HA，仅controller2执行
source ~/admin-openstack.sh

# install gnocchi
# gnocchi DATABASE
mysql -h controller -u root -pinnobase -e"create database gnocchi;
grant all privileges on gnocchi.* to 'gnocchi'@'localhost' identified by 'gnocchi';
grant all privileges on gnocchi.* to 'gnocchi'@'%' identified by 'gnocchi';
flush privileges;"

mysql -h controller -u root -pinnobase -e"show databases;"

openstack user create --domain default --password=gnocchi gnocchi
openstack role add --project service --user gnocchi admin
openstack service create --name gnocchi --description "Metric Service" metric

openstack endpoint create --region RegionOne metric public http://controller:8041
openstack endpoint create --region RegionOne metric internal http://controller:8041
openstack endpoint create --region RegionOne metric admin http://controller:8041

yum install openstack-gnocchi-api openstack-gnocchi-metricd python2-gnocchiclient -y
cp /etc/gnocchi/gnocchi.conf{,.bak}

settingfile=/etc/gnocchi/gnocchi.conf
#[DEFAULT]
openstack-config --set $settingfile DEFAULT log_dir /var/log/gnocchi
#[api]
openstack-config --set $settingfile api auth_mode keystone
#[indexer]
openstack-config --set $settingfile indexer url mysql+pymysql://gnocchi:gnocchi@controller/gnocchi
#[storage]
openstack-config --set $settingfile storage driver file
openstack-config --set $settingfile storage file_basepath /var/lib/gnocchi
#[keystone_authtoken]
openstack-config --set $settingfile keystone_authtoken auth_uri http://controller:5000
openstack-config --set $settingfile keystone_authtoken auth_url http://controller:35357
openstack-config --set $settingfile keystone_authtoken memcached_servers controller1:11211,controller2:11211,controller3:11211
openstack-config --set $settingfile keystone_authtoken auth_type password
openstack-config --set $settingfile keystone_authtoken project_domain_name default
openstack-config --set $settingfile keystone_authtoken user_domain_name default
openstack-config --set $settingfile keystone_authtoken project_name service
openstack-config --set $settingfile keystone_authtoken username gnocchi
openstack-config --set $settingfile keystone_authtoken password gnocchi

#创建http监听端口
echo 'Listen controller2:8041

<VirtualHost *:8041>
  ServerName controller2

  ## Vhost docroot
  DocumentRoot "/var/www/cgi-bin/gnocchi"

  ## Directories, there should at least be a declaration for /var/www/cgi-bin/gnocchi

  <Directory "/var/www/cgi-bin/gnocchi">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Require all granted
  </Directory>

  ## Logging
  ErrorLog "/var/log/httpd/gnocchi_wsgi_error.log"
  ServerSignature Off
  CustomLog "/var/log/httpd/gnocchi_wsgi_access.log" combined
  SetEnvIf X-Forwarded-Proto https HTTPS=1
  WSGIApplicationGroup %{GLOBAL}
  WSGIDaemonProcess gnocchi display-name=gnocchi_wsgi group=gnocchi processes=8 threads=8 user=gnocchi
  WSGIProcessGroup gnocchi
  WSGIScriptAlias / "/var/www/cgi-bin/gnocchi/app"
</VirtualHost>'>/etc/httpd/conf.d/10-gnocchi_wsgi.conf

mkdir /var/www/cgi-bin/gnocchi/
cp /usr/lib/python2.7/site-packages/gnocchi/rest/app.wsgi /var/www/cgi-bin/gnocchi/app
chown -R gnocchi:gnocchi /var/www/cgi-bin/gnocchi
#初始化数据库
su -s /bin/bash gnocchi -c "gnocchi-upgrade"
mysql -h controller -u root -pinnobase -e"use gnocchi;show tables;"
#重启 httpd 以加载 gnocchi
systemctl restart httpd
systemctl enable openstack-gnocchi-metricd
systemctl start openstack-gnocchi-metricd

netstat -anop|grep 8041

echo '
#gnocchi_API_cluster
listen gnocchi_API_cluster
bind controller:8041
#balance source
option tcpka
option httpchk
option tcplog
#server controller1 controller1:8041 check inter 2000 rise 2 fall 5
server controller2 controller2:8041 check inter 2000 rise 2 fall 5
#server controller3 controller3:8041 check inter 2000 rise 2 fall 5
'>>/etc/haproxy/haproxy.cfg
systemctl restart haproxy.service

# aodh
mysql -h controller -u root -pinnobase -e"create database aodh;
grant all privileges on aodh.* to 'aodh'@'localhost' identified by 'aodh';
grant all privileges on aodh.* to 'aodh'@'%' identified by 'aodh';
flush privileges;"

openstack user create --domain default --password=aodh aodh
openstack role add --project service --user aodh admin
openstack service create --name aodh --description "Telemetry alarming" alarming

openstack endpoint create --region RegionOne alarming public http://controller:8042
openstack endpoint create --region RegionOne alarming internal http://controller:8042
openstack endpoint create --region RegionOne alarming admin http://controller:8042

yum install openstack-aodh-api \
  openstack-aodh-evaluator openstack-aodh-notifier \
  openstack-aodh-listener openstack-aodh-expirer \
  python-aodhclient -y

cp /etc/aodh/aodh.conf{,.bak}
settingfile=/etc/aodh/aodh.conf
#[DEFAULT]
openstack-config --set $settingfile DEFAULT transport_url rabbit://openstack:openstack@controller:5673
openstack-config --set $settingfile DEFAULT auth_strategy keystone

#[database]
openstack-config --set $settingfile database connection mysql+pymysql://aodh:aodh@controller/aodh

#[keystone_authtoken]
openstack-config --set $settingfile keystone_authtoken auth_uri http://controller:5000
openstack-config --set $settingfile keystone_authtoken auth_url http://controller:35357
openstack-config --set $settingfile keystone_authtoken memcached_servers controller1:11211,controller2:11211,controller3:11211
openstack-config --set $settingfile keystone_authtoken auth_type password
openstack-config --set $settingfile keystone_authtoken project_domain_name default
openstack-config --set $settingfile keystone_authtoken user_domain_name default
openstack-config --set $settingfile keystone_authtoken project_name service
openstack-config --set $settingfile keystone_authtoken username aodh
openstack-config --set $settingfile keystone_authtoken password aodh

#[service_credentials]
openstack-config --set $settingfile service_credentials auth_type password
openstack-config --set $settingfile service_credentials auth_url http://controller:5000/v3
openstack-config --set $settingfile service_credentials project_domain_name default
openstack-config --set $settingfile service_credentials user_domain_name default
openstack-config --set $settingfile service_credentials project_name service
openstack-config --set $settingfile service_credentials username aodh
openstack-config --set $settingfile service_credentials password aodh
openstack-config --set $settingfile service_credentials interface internalURL
openstack-config --set $settingfile service_credentials region_name RegionOne

#aodh http server
echo 'Listen controller2:8042

<VirtualHost *:8042>
  ServerName controller2

  ## Vhost docroot
  DocumentRoot "/var/www/cgi-bin/aodh"

  ## Directories, there should at least be a declaration for /var/www/cgi-bin/aodh

  <Directory "/var/www/cgi-bin/aodh">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Require all granted
  </Directory>

  ## Logging
  ErrorLog "/var/log/httpd/aodh_wsgi_error.log"
  ServerSignature Off 
  CustomLog "/var/log/httpd/aodh_wsgi_access.log" combined 
  SetEnvIf X-Forwarded-Proto https HTTPS=1
  WSGIApplicationGroup %{GLOBAL}
  WSGIDaemonProcess aodh display-name=aodh_wsgi group=aodh processes=2 threads=2 user=aodh
  WSGIProcessGroup aodh
  WSGIScriptAlias / "/var/www/cgi-bin/aodh/app"
</VirtualHost>'>/etc/httpd/conf.d/10-aodh_wsgi.conf

mkdir /var/www/cgi-bin/aodh/
cp /usr/lib/python2.7/site-packages/aodh/api/app.wsgi /var/www/cgi-bin/aodh/app
chown -R aodh:aodh /var/www/cgi-bin/aodh

aodh-dbsync
mysql -h controller -u root -pinnobase -e"use aodh;show tables;"

systemctl restart httpd
systemctl enable openstack-aodh-evaluator.service openstack-aodh-notifier.service openstack-aodh-listener.service
systemctl start openstack-aodh-evaluator.service openstack-aodh-notifier.service openstack-aodh-listener.service

netstat -anop|grep 8042

echo '
#aodh_API_cluster
listen aodh_API_cluster
bind controller:8042
#balance source
option tcpka
option httpchk
option tcplog
#server controller1 controller1:8042 check inter 2000 rise 2 fall 5
server controller2 controller2:8042 check inter 2000 rise 2 fall 5
#server controller3 controller3:8042 check inter 2000 rise 2 fall 5
'>>/etc/haproxy/haproxy.cfg

#Panko 
mysql -h controller -u root -pinnobase -e"create database panko;
grant all privileges on panko.* to 'panko'@'localhost' identified by 'panko';
grant all privileges on panko.* to 'panko'@'%' identified by 'panko';
flush privileges;"

openstack user create --domain default --password=panko panko
openstack role add --project service --user panko admin
openstack service create --name panko --description "OpenStack Event Service" event

openstack endpoint create --region RegionOne event public http://controller:8779
openstack endpoint create --region RegionOne event internal http://controller:8779
openstack endpoint create --region RegionOne event admin http://controller:8779

yum install openstack-panko-api openstack-panko-common -y

cp /etc/panko/panko.conf{,.bak}
settingfile=/etc/panko/panko.conf

#[DEFAULT]
openstack-config --set $settingfile DEFAULT auth_strategy keystone
openstack-config --set $settingfile DEFAULT log_file /var/log/panko/panko.log
openstack-config --set $settingfile DEFAULT log_dir /var/log/panko

#[api]
openstack-config --set $settingfile api host 0.0.0.0
openstack-config --set $settingfile api port 8779
openstack-config --set $settingfile api workers 2
openstack-config --set $settingfile api max_limit 1000

#[database]
openstack-config --set $settingfile database connection mysql+pymysql://panko:panko@controller/panko

#[keystone_authtoken]
openstack-config --set $settingfile keystone_authtoken auth_uri http://controller:5000
openstack-config --set $settingfile keystone_authtoken auth_url http://controller:35357
openstack-config --set $settingfile keystone_authtoken memcached_servers controller1:11211,controller2:11211,controller3:11211
openstack-config --set $settingfile keystone_authtoken auth_type password
openstack-config --set $settingfile keystone_authtoken project_domain_name default
openstack-config --set $settingfile keystone_authtoken user_domain_name default
openstack-config --set $settingfile keystone_authtoken project_name service
openstack-config --set $settingfile keystone_authtoken username panko
openstack-config --set $settingfile keystone_authtoken password panko


echo 'Listen controller2:8779

<VirtualHost *:8779>
  ServerName controller2

  ## Vhost docroot
  DocumentRoot "/var/www/cgi-bin/panko"

  ## Directories, there should at least be a declaration for /var/www/cgi-bin/panko

  <Directory "/var/www/cgi-bin/panko">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Require all granted
  </Directory>

  ## Logging
  ErrorLog "/var/log/httpd/panko_wsgi_error.log"
  ServerSignature Off 
  CustomLog "/var/log/httpd/panko_wsgi_access.log" combined 
  SetEnvIf X-Forwarded-Proto https HTTPS=1
  WSGIApplicationGroup %{GLOBAL}
  WSGIDaemonProcess panko display-name=panko_wsgi group=panko processes=2 threads=2 user=panko
  WSGIProcessGroup panko
  WSGIScriptAlias / "/var/www/cgi-bin/panko/app"
</VirtualHost>'>/etc/httpd/conf.d/10-panko_wsgi.conf

mkdir /var/www/cgi-bin/panko/
cp /usr/lib/python2.7/site-packages/panko/api/app.wsgi /var/www/cgi-bin/panko/app
chown -R panko:panko /var/www/cgi-bin/panko/

# init panko db
/usr/bin/panko-dbsync
mysql -h controller -u root -pinnobase -e"use panko;show tables;"
# restart httpd to load panko api
systemctl restart httpd
netstat -anop|grep 8779
echo '
#panko_API_cluster
listen panko_API_cluster
bind controller:8779
#balance source
option tcpka
option httpchk
option tcplog
#server controller1 controller1:8779 check inter 2000 rise 2 fall 5
server controller2 controller2:8779 check inter 2000 rise 2 fall 5
#server controller3 controller3:8779 check inter 2000 rise 2 fall 5
'>>/etc/haproxy/haproxy.cfg
#Ceilometer服务

# create the service credentials
openstack user create --domain default --password=ceilometer ceilometer
openstack role add --project service --user ceilometer admin
openstack service create --name ceilometer --description "Telemetry metering" metering

openstack endpoint create --region RegionOne metering public http://controller:8777
openstack endpoint create --region RegionOne metering internal http://controller:8777
openstack endpoint create --region RegionOne metering admin http://controller:8777

yum install openstack-ceilometer-api \
  openstack-ceilometer-collector openstack-ceilometer-notification \
  openstack-ceilometer-central python-ceilometerclient  -y

cp /etc/ceilometer/ceilometer.conf{,.bak}
settingfile=/etc/ceilometer/ceilometer.conf

#[DEFAULT]
openstack-config --set $settingfile DEFAULT transport_url rabbit://openstack:openstack@controller:5673
openstack-config --set $settingfile DEFAULT auth_strategy keystone
openstack-config --set $settingfile DEFAULT meter_dispatchers gnocchi
openstack-config --set $settingfile DEFAULT event_dispatchers gnocchi

#[dispatcher_gnocchi]
openstack-config --set $settingfile dispatcher_gnocchi filter_service_activity False
openstack-config --set $settingfile dispatcher_gnocchi archive_policy high
openstack-config --set $settingfile dispatcher_gnocchi resources_definition_file gnocchi_resources.yaml
openstack-config --set $settingfile dispatcher_gnocchi url http://controller:8041

#[keystone_authtoken]
openstack-config --set $settingfile keystone_authtoken auth_uri http://controller:5000
openstack-config --set $settingfile keystone_authtoken auth_url http://controller:35357
openstack-config --set $settingfile keystone_authtoken memcached_servers controller1:11211,controller2:11211,controller3:11211
openstack-config --set $settingfile keystone_authtoken auth_type password
openstack-config --set $settingfile keystone_authtoken project_domain_name default
openstack-config --set $settingfile keystone_authtoken user_domain_name default
openstack-config --set $settingfile keystone_authtoken project_name service
openstack-config --set $settingfile keystone_authtoken username ceilometer
openstack-config --set $settingfile keystone_authtoken password ceilometer
openstack-config --set $settingfile keystone_authtoken service_token_roles_required True

#[service_credentials]
openstack-config --set $settingfile service_credentials auth_type password
openstack-config --set $settingfile service_credentials auth_url http://controller:5000/v3
openstack-config --set $settingfile service_credentials project_domain_name default
openstack-config --set $settingfile service_credentials user_domain_name default
openstack-config --set $settingfile service_credentials project_name service
openstack-config --set $settingfile service_credentials username ceilometer
openstack-config --set $settingfile service_credentials password ceilometer
openstack-config --set $settingfile service_credentials interface internalURL
openstack-config --set $settingfile service_credentials region_name RegionOne


echo 'Listen controller2:8777

<VirtualHost *:8777>
  ServerName controller2

  ## Vhost docroot
  DocumentRoot "/var/www/cgi-bin/ceilometer"

  ## Directories, there should at least be a declaration for /var/www/cgi-bin/ceilometer

  <Directory "/var/www/cgi-bin/ceilometer">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Require all granted
  </Directory>

  ## Logging
  ErrorLog "/var/log/httpd/ceilometer_wsgi_error.log"
  ServerSignature Off 
  CustomLog "/var/log/httpd/ceilometer_wsgi_access.log" combined 
  SetEnvIf X-Forwarded-Proto https HTTPS=1
  WSGIApplicationGroup %{GLOBAL}
  WSGIDaemonProcess ceilometer display-name=ceilometer_wsgi group=ceilometer processes=2 threads=2 user=ceilometer
  WSGIProcessGroup ceilometer
  WSGIScriptAlias / "/var/www/cgi-bin/ceilometer/app"
</VirtualHost>'>/etc/httpd/conf.d/10-ceilometer_wsgi.conf

mkdir /var/www/cgi-bin/ceilometer/
cp /usr/lib/python2.7/site-packages/ceilometer/api/app.wsgi /var/www/cgi-bin/ceilometer/app
chown -R ceilometer:ceilometer /var/www/cgi-bin/ceilometer/

ceilometer-upgrade --skip-metering-database

systemctl restart httpd
netstat -anop|grep 8777

echo '
#ceilometer_API_cluster
listen ceilometer_API_cluster
bind controller:8777
#balance source
option tcpka
option httpchk
option tcplog
#server controller1 controller1:8777 check inter 2000 rise 2 fall 5
server controller2 controller2:8777 check inter 2000 rise 2 fall 5
#server controller3 controller3:8777 check inter 2000 rise 2 fall 5
'>>/etc/haproxy/haproxy.cfg

systemctl enable openstack-ceilometer-notification.service \
  openstack-ceilometer-central.service \
  openstack-ceilometer-collector.service
systemctl start openstack-ceilometer-notification.service \
  openstack-ceilometer-central.service \
  openstack-ceilometer-collector.service